package com.xmg.p2p.base.controller;

import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.domain.RealAuth;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.query.PageResult;
import com.xmg.p2p.base.query.UserFileQueryObject;
import com.xmg.p2p.base.service.IAccountService;
import com.xmg.p2p.base.service.IRealAuthService;
import com.xmg.p2p.base.service.IUserFileService;
import com.xmg.p2p.base.service.IUserinfoService;
import com.xmg.p2p.base.util.BidConst;
import com.xmg.p2p.base.util.UserContext;
import com.xmg.p2p.business.domain.BidRequest;
import com.xmg.p2p.business.service.IBidRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * lcd  2020/5/13
 * Description:
 */
@Controller
public class BorrowController {
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IUserinfoService userinfoService;
    @Autowired
    private IRealAuthService realAuthService;
    @Autowired
    private IUserFileService userFileService;
    @RequestMapping("borrow")
    public String borrowIndex(Model model) {
        Logininfo logininfo = UserContext.getLogininfo();
        if (logininfo == null) {
            return "redirect:borrow.html";
        }else{
            model.addAttribute("account", accountService.findById(logininfo.getId()));
            model.addAttribute("userinfo", userinfoService.findById(logininfo.getId()));
            model.addAttribute("creditBorrowScore", BidConst.BASE_BORROW_SCORE);
            return "borrow";
        }
    }

    /**
     * 涉及到安全权限,所以要保证
     * 导向到申请借款
     * 保证4个条件
     * 保证用户状态码 是否申请流程
     * @param model
     * @return
     */
    @RequestMapping("borrowInfo")
    public String borrowInfo(Model model) {
        Logininfo current = UserContext.getLogininfo();
        boolean b = bidRequestService.canApplyBindRequest(current.getId());
        if (b) {
            model.addAttribute("minBidRequestAmount", BidConst.SMALLEST_BIDREQUEST_AMOUNT);
            model.addAttribute("account", accountService.findById(current.getId()));
            model.addAttribute("minBidAmount", BidConst.SMALLEST_BID_AMOUNT);
            return "borrow_apply";
        }else {
            return "borrow_apply_result";
        }

    }
    @RequestMapping("borrow_apply")
    public String borrow_apply(BidRequest bidRequest) {
        bidRequestService.apply(bidRequest);
        return "redirect:borrowInfo.do";
    }

    //前台借款明细,没登陆也能看
    @RequestMapping("borrow_info")
    public String borrowInfoDetails(Long id,Model model){
        BidRequest bidRequest = bidRequestService.findById(id);
        if (bidRequest != null) {//完整的话可以判断一下这个标的那三个状态
            Userinfo applier = userinfoService.findById(bidRequest.getCreateUser().getId());
            RealAuth realAuth = realAuthService.findById(applier.getRealAuthId());
            model.addAttribute("realAuth", realAuth);
            UserFileQueryObject qo = new UserFileQueryObject();
            qo.setApplierId(applier.getId());
            qo.setPageSize(-1);
            PageResult pageResult = userFileService.queryForPage(qo);
            model.addAttribute("userFiles", pageResult.getListData());
            model.addAttribute("bidRequest", bidRequest);
            model.addAttribute("userInfo", applier);
            if (UserContext.getLogininfo() != null) {
                if (UserContext.getLogininfo().getId().equals(applier.getId())) {
                    model.addAttribute("self", true);
                }else {
                    model.addAttribute("self", false);
                    model.addAttribute("account", accountService.getCurrent());
                }
            }else {
                model.addAttribute("self", false);
            }
        }
        // realAuth
        // userFiles
        // bidRequest

        // account
        // self当前用户是否是借款人自己
        return "borrow_info";
    }
}
